package com.efounder.frame.xmlparse;

import android.util.Log;

import com.core.xml.StubObject;
import com.efounder.frame.utils.EFAppAccountUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class EFRegistry {
	public EFRegistry(){
	}
	/**key:元素名（有时候也是id），values：List<StubObject>**/
	private static Map<String, Object> registryStore = new HashMap<String, Object>();
	/**key:id,value:StubObject**/
	private static Map<String, Object> allRegistryStore = new HashMap<String, Object>();
	/**key:id,value:两个position**/
	private static Map<String, int[]> fragmentPositionStore = new HashMap<>();

	public static void clearRegistry(){
		registryStore.clear();
		allRegistryStore.clear();
	}

	private static ArrayList<Object> createEntryList(String entryKey){
		if(registryStore.get(entryKey) == null){
			registryStore.put(entryKey, new ArrayList<Object>());
		}
		return (ArrayList<Object>)registryStore.get(entryKey);
	}

	public static void regObject(String entryKey, StubObject stub){
		ArrayList<Object> regArray = createEntryList(entryKey);
		regArray.add(stub);
	}

	public static List<StubObject> getRegEntryList(String entryKey){
		//如果没有数据，先初始化Registry
		if (registryStore.size() == 0){
//			File xmlFile = new File(Constants.PACKAGE_DIR + "/Tab_Menu.xml");
			File xmlFile = new File(EFAppAccountUtils.getAppAccountAppFramePath());
			EFRegistryUtil.initRegistry(xmlFile);
		}
		if(registryStore.get(entryKey) != null){
			return (ArrayList<StubObject>)registryStore.get(entryKey);
		}
		return null;
	}

	public static void regNode(String idKey, StubObject attrStubObject){
		allRegistryStore.put(idKey,attrStubObject);
	}

	/***
	 * 根据id获取对应的元素属性map
	 * @param id
	 * @return
     */
	public static StubObject getRegNode(String id){
		//如果没有数据，先初始化Registry
		if (allRegistryStore.size() == 0){
//			File xmlFile = new File(Constants.PACKAGE_DIR + "/Tab_Menu.xml");
			File xmlFile = new File(EFAppAccountUtils.getAppAccountAppFramePath());
			EFRegistryUtil.initRegistry(xmlFile);
		}
		return (StubObject) allRegistryStore.get(id);
	}

	public static Map<String, Object> getRegistryStore() {
		return registryStore;
	}

	public static Map<String, Object> getAllRegistryStore() {
		return allRegistryStore;
	}

	public static void setRegistryStore(Map<String, Object> registryStore) {
		EFRegistry.registryStore = registryStore;
	}

	public static void setAllRegistryStore(Map<String, Object> allRegistryStore) {
		EFRegistry.allRegistryStore = allRegistryStore;
	}

	/**
	 * XXX-根据id获取Fragment的位置信息
	 * @param id
	 * @return
     */
	public static int[] getFragmentPositions(String id){
		int[] positions = fragmentPositionStore.get(id);
		if (positions == null){
			Log.e("--","Tab_Menu.xml中没有找到id=" + id + "的配置信息，请检查配置文件");
		}
		return positions;
	}

	/**
	 * XXX-初始化Fragment位置信息
	 */
	protected static void initFragmentPositionStore(){
		List<StubObject> oneLevelList = getRegEntryList(EFXmlConstants.ELE_MENU_ROOT);
		for (int i=0 ; i<oneLevelList.size(); i++){
			StubObject oneLevelStub = oneLevelList.get(i);
			String oneLevelId = oneLevelStub.getString(EFXmlConstants.ATTR_ID,"");
			int[] oneLevelPositions = new int[2];
			oneLevelPositions[0] = i;
			oneLevelPositions[1] = oneLevelStub.getInt(EFXmlConstants.ATTR_INIT_POSITION,0);
			fragmentPositionStore.put(oneLevelId,oneLevelPositions);

			List<StubObject> twoLevelList = getRegEntryList(oneLevelId);
			for (int j=0 ; j<twoLevelList.size(); j++){
				StubObject twoLevelStub = twoLevelList.get(j);
				String twoLevelId = twoLevelStub.getString(EFXmlConstants.ATTR_ID,"");
				int[] twoLevelPositions = new int[2];
				twoLevelPositions[0] = i;
				twoLevelPositions[1] = j;
				fragmentPositionStore.put(twoLevelId,twoLevelPositions);
			}
		}
	}

}
